<?php namespace nimo\domains\master\controllers;

use BaseController;
use Input;
use View;
use Validator;
use Redirect;
use Request;
use Response;

use nimo\domains\master\models\Code as Code;

class CodeController extends BaseController {

    /**
     * Code Repository
     *
     * @var Code
     */
    protected $code;

    public function __construct(Code $code)
    {
        $this->code = $code;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $codes = $this->code->all();

        return View::make('nimo.domains.master.code.index', compact('codes'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('nimo.domains.master.code.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $input = Input::all();
        $validation = Validator::make($input, Code::$rules);

        if ($validation->passes())
        {
            if (!is_null($code = $this->code->create($input))) {
                return Redirect::route('nd.master.code.show', $code->getJointPk())
                    ->with('success', 'There were successed.');
            }

            return Redirect::route('nd.master.code.index');
        }

        return Redirect::route('nd.master.code.create')
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $code = $this->code->findByJointPkOrFail($id);

        return View::make('nimo.domains.master.code.show', compact('code'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $code = $this->code->findByJointPk($id);

        if (is_null($code))
        {
            return Redirect::route('nd.master.code.index');
        }

        return View::make('nimo.domains.master.code.edit', compact('code'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $input = array_except(Input::all(), '_method');
        $validation = Validator::make($input, Code::$rules);

        if ($validation->passes())
        {
            $code = $this->code->findByJointPk($id);
            $code->update($input);

            return Redirect::route('nd.master.code.show', $id)
                ->with('success', 'There were successed.');
        }

        return Redirect::route('nd.master.code.edit', $id)
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $this->code->findByJointPk($id)->delete();

        return Redirect::route('nd.master.code.index');
    }

}